package com.shenzhenai.composebasic

import android.os.Bundle
import android.util.Log
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.activity.enableEdgeToEdge
import androidx.compose.foundation.Image
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.shape.CircleShape
import androidx.compose.foundation.text.ClickableText
import androidx.compose.foundation.text.selection.SelectionContainer
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Translate
import androidx.compose.material3.Icon
import androidx.compose.material3.IconButton
import androidx.compose.material3.Scaffold
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.graphics.BlendMode
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.ColorFilter
import androidx.compose.ui.graphics.ImageBitmap
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.res.imageResource
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.text.SpanStyle
import androidx.compose.ui.text.buildAnnotatedString
import androidx.compose.ui.text.style.TextDecoration
import androidx.compose.ui.text.withStyle
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import com.shenzhenai.composebasic.components.PermissionSample
import com.shenzhenai.composebasic.ui.theme.DN_Compose_LearnTheme

class MainActivity : ComponentActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        enableEdgeToEdge()
        setContent {
            DN_Compose_LearnTheme {
                Scaffold(modifier = Modifier.fillMaxSize()) { innerPadding ->
//                    Greeting(
//                        name = "Android",
//                        modifier = Modifier.padding(innerPadding)
//                    )

                    Column(modifier = Modifier.padding(innerPadding)) {
                        weijueyeTest()
                        IconsSample()
                        ImageSample()
                        PermissionSample()
                    }

                }
            }
        }
    }


    @Composable
    fun weijueyeTest() {
        // 韦爵爷部分代码
        // 可点击文本
        // https://www.bilibili.com/video/BV1e5411o76A?vd_source=e07cc8cc94eb22bd76722071b03cd199&spm_id_from=333.788.videopod.sections
        ClickableText(text = buildAnnotatedString {
            // 这里类似 SpannedString
            append("Hello World")

            withStyle(
                style = SpanStyle(
                    color = Color.Red,
                    textDecoration = TextDecoration.Underline,
                    fontSize = 16.sp
                )
            ) {
                append("Jetpack Compose")
            }
        }) {
            // it 是标识点击的字符串的角标
            Log.e("young_test9090d", "it = ${it}")
        }

        // 可选择容器，就是可以选中文本，进行复制
        SelectionContainer {
            Text("Hello World")
        }

        // https://www.bilibili.com/video/BV1e5411o76A?vd_source=e07cc8cc94eb22bd76722071b03cd199&spm_id_from=333.788.videopod.sections
        val annotatedString = buildAnnotatedString {
            append("点击登录即代表您已知悉和同意")

            /**
             * pushStringAnnotation 和 pop 圈着的区域，都可以获取到 pushStringAnnotation 的参数；
             *
             * pushStringAnnotation 就是传入参数，然后直到调用 pop 之前到文本，都可以获取到 pushStringAnnotation 的参数
             */

            /**
             * pushStringAnnotation 和 pop 圈着的区域，都可以获取到 pushStringAnnotation 的参数；
             *
             * pushStringAnnotation 就是传入参数，然后直到调用 pop 之前到文本，都可以获取到 pushStringAnnotation 的参数
             */

            pushStringAnnotation("protocol", "https://www.baidu.com")
            withStyle(
                style = SpanStyle(
                    color = Color.Blue,
                    textDecoration = TextDecoration.Underline
                )
            ) {
                append("用户协议")
            }
            pop()

            append("和")

            pushStringAnnotation("privacy", "https://www.google.com/")
            withStyle(
                style = SpanStyle(
                    color = Color.Blue,
                    textDecoration = TextDecoration.Underline
                )
            ) {
                append("隐私政策")
            }
            pop()

        }

        ClickableText(text = annotatedString) { offset ->
            // 获取 pushStringAnnotation 的参数
            annotatedString.getStringAnnotations("protocol", start = offset, end = offset)
                .firstOrNull()?.let { annotation ->
                    Log.e("young_test9090d", "点击了用户协议 = ${annotation.item}")
                }

            annotatedString.getStringAnnotations("privacy", start = offset, end = offset)
                .firstOrNull()?.let { annotation ->
                    Log.e("young_test9090d", "点击了隐私政策 = ${annotation.item}")
                }


        }
    }

    @Composable
    fun IconsSample() {
        /**
         * icon的用法
         * https://www.bilibili.com/video/BV1RT4y1X7MK?vd_source=e07cc8cc94eb22bd76722071b03cd199&spm_id_from=333.788.videopod.sections
         */
        // 设置矢量图标
        Icon(imageVector = Icons.Default.Translate, contentDescription = null)

        // 设置本地资源
        Icon(
            painter = painterResource(id = R.drawable.qier),
            contentDescription = null,
            // 这里设置为使用原始的颜色，因为不设置默认会显示黑色，那么图片就是一张黑色的图片
            tint = Color.Unspecified,
        )

        Icon(
            bitmap = ImageBitmap.imageResource(resources, R.drawable.qier),
            contentDescription = null,
            tint = Color.Unspecified,
        )

        IconButton(onClick = {
            Log.e("young_test", "点击了icon")
        }) {
            Icon(
                bitmap = ImageBitmap.imageResource(resources, R.drawable.qier),
                contentDescription = null,
                tint = Color.Unspecified,
            )
        }

    }

    /**
     * Image 的使用案例
     */
    @Composable
    fun ImageSample() {
        /**
         *
         * https://www.bilibili.com/video/BV1bm4y1d7GS?vd_source=e07cc8cc94eb22bd76722071b03cd199&spm_id_from=333.788.videopod.sections
         */

        Image(
            painter = painterResource(id = R.drawable.qier),
            contentDescription = null,
            modifier = Modifier.size(50.dp),
            contentScale = ContentScale.Crop,
            // 设置颜色滤镜
            colorFilter = ColorFilter.tint(Color.Red, blendMode = BlendMode.Color)
        )
    }
}
